<?php

declare(strict_types=1);

namespace app\model;

use app\model\HonorType;

/**
 * @mixin \think\Model
 */
class Honor extends Base
{

    protected $schema = [
        "id" => "int",  // 
        "title" => "varchar",  // 标题
        "picture" => "varchar",  // 图片地址
        "status" => "tinyint",  // 状态
        "sort" => "smallint",  // 排序, 降序
        "type_id" => "int",  // 广告位
        "create_time" => "datetime",  // 
        "update_time" => "datetime",  // 
    ];

    public function type()
    {
        return $this->belongsTo('HonorType', 'type_id')->bind(['type_name' => 'name']);
    }

    public static function queryPage($params = [])
    {
        $limit = empty($params['limit']) ? 20 : (int)$params['limit'];
        $list = self::field('id,title,picture,sort,status,type_id,create_time,update_time')
            ->with(['type'])->order('id desc')->paginate(['list_rows' => $limit, 'query' => $params]);

        return $list;
    }

    public static function queryListGroup()
    {
        $list = self::where('status', 1)->field('id,title,picture,sort,status,type_id')->order('sort desc')->select();

        $types = HonorType::where('status', 1)->order('id asc')->select();

        foreach ($types as $value) {
            $li = [];
            foreach ($list as $val) {
                if ($val->type_id == $value->id) {
                    $li[] = $val;
                }
            }
            $value->list = $li;
        }

        return $types;
    }
}
